<?php
function d($arr) {
	echo '<pre>';
	print_r($arr);
	echo '</pre>';
}
function encodeFilename($filename) {
	$filename = preg_replace("/[^a-zA-Z0-9-.]/", "_", $filename);
	return $filename;
}
// function trim for all $_POST before save db
function trim_data($data) {
	foreach ($data as $key => $value) {
		if(is_array($value)) {
			$data[$key] = trim_data($value);
		}
		else {
			$data[$key] = trim($value);
		}
	}
	return $data;
}
function getErrors($model){
	$content = "";
	foreach($model->getErrors() as $errors)
	{
		foreach($errors as $error)
		{
			if($error!='')
			$content .= $error . "<br>";
		}
	}
	return $content;
}
function checkAdmin($userID) {
	$user_role = UserRole::model()->findAllByAttributes(array('userID'=>$userID));
	$ID = Role::model()->findByAttributes(array('role'=>'admin'))->ID;
		foreach ($user_role as $item) {
			if($ID == $item->roleID) {
				return true;				
			}
		}
	return false;
}
function checkResolver($userID) {
	$user_role = UserRole::model()->findAllByAttributes(array('userID'=>$userID));
	$ID = Role::model()->findByAttributes(array('role'=>'resolver'))->ID;
		foreach ($user_role as $item) {
			if($ID == $item->roleID) {
				return true;				
			}
		}
	return false;
}
function checkChecker($userID) {
	$user_role = UserRole::model()->findAllByAttributes(array('userID'=>$userID));
	$ID = Role::model()->findByAttributes(array('role'=>'checker'))->ID;
		foreach ($user_role as $item) {
			if($ID == $item->roleID) {
				return true;				
			}
		}
	return false;
}
function checkEditor($userID) {
	$user_role = UserRole::model()->findAllByAttributes(array('userID'=>$userID));
	$ID = Role::model()->findByAttributes(array('role'=>'editor'))->ID;
		foreach ($user_role as $item) {
			if($ID == $item->roleID) {
				return true;				
			}
		}
	return false;
}
function checkStudent($userID) {
	$user_role = UserRole::model()->findAllByAttributes(array('userID'=>$userID));
	$ID = Role::model()->findByAttributes(array('role'=>'user'))->ID;
		foreach ($user_role as $item) {
			if($ID == $item->roleID) {
				return true;				
			}
		}
	return false;
}